package com.xjc.infra.code;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.xjc.infra.exception.IFSException;


/**
 * 重构代码复制工具:
 * @author user
 *
 */
public class CodeCopyForDXQUtil {
	/* 如：
	P2P\IFSP2P\src\main\java\com\ifs\p2p\ci\biz\CustCommBiz.java
	portal\IFSPortal\src\main\webapp\WEB-INF\jsp\homePage\auto-bid.jsp
	core\IFSCore\src\main\java\com\ifs\core\loan\biz\LoanRepaymentBiz.java
	*/
	public static String sourceFilePath="D:\\tmp\\FileList.txt";

	// public static String source="D:\\projects\\DXQ_Team\\coding\\P2P\\";
	
	public static String source= "D:/projects/上上签/";

	public static String targetServer="D:\\tmp\\server\\";
	public static String targetCode="D:\\tmp\\code\\";
	public static int successCopyCode = 0;
	
	public static void main(String[] args) {
		System.out.println("复制开始**********");
		copyCode(source,targetCode,false); // 复制代码
		//copyCode(source,targetServer,true); // 复制编译文件
		System.out.println("复制结束**********");
		System.out.println("******************");
		System.out.println("成功复制代码(个)：" + successCopyCode);
	}
	
	/**
	 * @param source
	 * @param target
	 * @param copyCodeFlag false： 代码复制  true:  编制资源和编译文件
	 */
	public static void copyCode(String source,String target,boolean copyCodeFlag){
		File sourcePath= null;
		File targetPath= null;
		String filePath = null;
		List<String> notExitFileList = new ArrayList<>(); // 源文件不存在列表
		try {
			CodeCopyForDXQUtil copy = new CodeCopyForDXQUtil();
			
			FileReader reader = new FileReader(sourceFilePath);
			BufferedReader br = new BufferedReader(reader);
			filePath = null;
			while ((filePath = br.readLine()) != null) {
				if(filePath.startsWith("#"))
				{
					continue ;
				}
				
				filePath = filePath.trim();
				filePath = filePath.replaceAll("/", "\\\\");
				if(filePath == null || filePath.equals("")){
					continue;
				}
			
				if(!new File(source + filePath).exists()){
					notExitFileList.add(source + filePath);
					continue;
				}
				if(new File(source + filePath).isDirectory()){
					continue;
				}
				if(filePath.contains("src\\test\\java") && copyCodeFlag == true){
					continue;
				}
				if(filePath.contains("pom.xml") && copyCodeFlag == true){
					continue;
				}
				if(copyCodeFlag)
				{
					sourcePath=new File(source+filePath);
				  if(filePath.endsWith(".jsp")||filePath.endsWith(".js")  || filePath.endsWith(".css") || filePath.endsWith(".png"))  				    
				  {
				    String tempTargetPath= null;
				    if(filePath.startsWith("portal\\IFSPortal"))
					{
				    	String f1 = filePath.replace("portal\\IFSPortal\\src\\main\\webapp\\WEB-INF\\", "WEB-INF\\");
				    	f1 = f1.replace("portal\\IFSPortal\\src\\main\\webapp\\", "");
				        tempTargetPath="ROOT\\"+f1;						   
					    targetPath=new File(target+tempTargetPath);
					}else if(filePath.startsWith("org\\Org"))
					{
				    	tempTargetPath= "org\\WEB-INF\\"+filePath.replace("org\\Org\\src\\main\\webapp\\WEB-INF\\", "");						   
					    targetPath=new File(target+tempTargetPath);
					}else if(filePath.startsWith("P2P\\IFSFrameServer"))
					{
				    	tempTargetPath= "IFSFrameServer\\WEB-INF\\"+filePath.replace("P2P\\IFSFrameServer\\src\\main\\webapp\\WEB-INF\\", "");					   
					    targetPath=new File(target+tempTargetPath);
					}else{
						 throw new IFSException(new Exception(),"999999","路径错误\n:" + filePath);
					}
				    
				   
				    targetPath=new File(target+tempTargetPath);
				      
				    
				    copy.copyFolder(sourcePath,targetPath); 
				  }else if(filePath.endsWith(".java") || filePath.endsWith(".json") 						     
						     || filePath.endsWith(".properties") || filePath.endsWith(".xml") || filePath.endsWith(".sqls") )
						    
				  {
					  String tempTargetPath= null;
					  String tempSourcePath = null;
					  filePath=filePath.replace(".java", ".class");
					  
					 if(filePath.startsWith("portal\\IFSPortal\\"))
					  {
						  String f1 = filePath.replace("portal\\IFSPortal\\src\\main\\java", "");
						  f1 = f1.replace("portal\\IFSPortal\\src\\main\\resources", "");
						  tempTargetPath="ROOT\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);
					      tempTargetPath="ROOT\\WEB-INF\\classes"+filePath.replace("portal\\IFSPortal\\src\\main\\java", "");		
					      
					      tempSourcePath=filePath.replace("portal\\IFSPortal\\src\\main\\java", "portal\\IFSPortal\\target\\classes");
						  sourcePath = new File(source + tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if(filePath.startsWith("portal\\IFS-Spring\\"))
					  {
						  String f1 = filePath.replace("portal\\IFS-Spring\\src\\main\\java", "");
						  f1 = f1.replace("portal\\IFS-Spring\\src\\main\\resources", "");
						  tempTargetPath="IFS-Spring\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);
					      tempTargetPath="IFS-Spring\\WEB-INF\\classes"+filePath.replace("portal\\IFS-Spring\\src\\main\\java", "");		
					      
					      tempSourcePath=filePath.replace("portal\\IFS-Spring\\src\\main\\java", "portal\\IFS-Spring\\target\\classes");
						  sourcePath = new File(source + tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if(filePath.startsWith("org\\Org\\"))
					  {
						  String f1 = filePath.replace("org\\Org\\src\\main\\java", "");
						  f1 = f1.replace("org\\Org\\src\\main\\resources", "");
						  tempTargetPath= "org\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("org\\Org\\src\\main\\java", "org\\Org\\target\\classes");	
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if( filePath.startsWith("P2P\\IFSWorkflow\\"))
					  {
						  
						  String f1 = filePath.replace("P2P\\IFSWorkflow\\src\\main\\java", "");
						  f1 = f1.replace("P2P\\IFSWorkflow\\src\\main\\resources", "");
						  tempTargetPath= "IFSFrameServer\\WEB-INF\\classes" + f1;						   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("P2P\\IFSWorkflow\\src\\main\\java", "IFSWorkflow\\target\\classes");	
					      if(filePath.contains("pom.xml")){
					    	  sourcePath=new File(source +tempSourcePath);
						  }else{
							  sourcePath=new File(source + "P2P\\" +tempSourcePath);
						  }
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("P2P\\IFSP2P\\") )
					  {						 
						  String f1 = filePath.replace("P2P\\IFSP2P\\src\\main\\java", "");
						  f1 = f1.replace("P2P\\IFSP2P\\src\\main\\resources", "");
						  tempTargetPath= "IFSFrameServer\\WEB-INF\\classes"+f1;					   
					      targetPath=new File(target+tempTargetPath);
					      
						  tempSourcePath=filePath.replace("P2P\\IFSP2P\\src\\main\\java", "IFSP2P\\target\\classes");	
						  if(filePath.contains("pom.xml")){
					    	  sourcePath=new File(source +tempSourcePath);
						  }else{
							  sourcePath=new File(source + "P2P\\" +tempSourcePath);
						  }
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("P2P\\IFSFrame\\") )
					  {						 
						  String f1 = filePath.replace("P2P\\IFSFrame\\src\\main\\java", "");
						  f1 = f1.replace("P2P\\IFSFrame\\src\\main\\resources", "");
						  tempTargetPath= "IFSFrameServer\\WEB-INF\\classes"+f1;					   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("P2P\\IFSFrame\\src\\main\\java", "IFSFrame\\target\\classes");
					      if(filePath.contains("pom.xml")){
					    	  sourcePath=new File(source +tempSourcePath);
						  }else{
							  sourcePath=new File(source + "P2P\\" +tempSourcePath);
						  }
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("P2P\\IFSInteractive\\") )
					  {						 
						  String f1 = filePath.replace("P2P\\IFSInteractive\\src\\main\\java", "");
						  f1 = f1.replace("P2P\\IFSInteractive\\src\\main\\resources", "");
						  tempTargetPath= "IFSFrameServer\\WEB-INF\\classes"+f1;					   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("P2P\\IFSInteractive\\src\\main\\java", "IFSInteractive\\target\\classes");
					      if(filePath.contains("pom.xml")){
					    	  sourcePath=new File(source +tempSourcePath);
						  }else{
							  sourcePath=new File(source + "P2P\\" +tempSourcePath);
						  }
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("P2P\\IFSParty\\") )
					  {						 
						  String f1 = filePath.replace("P2P\\IFSParty\\src\\main\\java", "");
						  f1 = f1.replace("P2P\\IFSParty\\src\\main\\resources", "");
						  tempTargetPath= "IFSFrameServer\\WEB-INF\\classes"+f1;					   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("P2P\\IFSParty\\src\\main\\java", "IFSParty\\target\\classes");
					      if(filePath.contains("pom.xml")){
					    	  sourcePath=new File(source +tempSourcePath);
						  }else{
							  sourcePath=new File(source + "P2P\\" +tempSourcePath);
						  }
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("P2P\\IFSFrameServer\\"))
					  {						 
						  String f1 = filePath.replace("P2P\\IFSFrameServer\\src\\main\\java", "");
						  f1 = f1.replace("P2P\\IFSFrameServer\\src\\main\\resources", "");
						  tempTargetPath= "IFSFrameServer\\WEB-INF\\classes"+f1;			
						  
					      targetPath=new File(target+tempTargetPath);
					      tempSourcePath=filePath.replace("P2P\\IFSFrameServer\\src\\main\\java", "P2P\\IFSFrameServer\\target\\classes");
					     // tempSourcePath=tempSourcePath.replace("P2P\\IFSFrameServer\\src\\main\\resources", "IFSFrameServer\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("core\\IFSXface\\"))
					  {						 
						  String f1 = filePath.replace("core\\IFSXface\\src\\main\\java", "");
						  f1 = f1.replace("core\\IFSXface\\src\\main\\resources", "");
						  tempTargetPath= "IFSXface\\WEB-INF\\classes"+f1;			
						  
					      targetPath=new File(target+tempTargetPath);
					      tempSourcePath=filePath.replace("core\\IFSXface\\src\\main\\java", "core\\IFSXface\\target\\classes");
					      tempSourcePath=tempSourcePath.replace("core\\IFSXface\\src\\main\\resources", "core\\IFSXface\\target\\classes");
					      sourcePath=new File(source+ "" +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("core\\IFSCore\\"))
					  {						 
						  String f1 = filePath.replace("core\\IFSCore\\src\\main\\java", "");
						  f1 = f1.replace("core\\IFSCore\\src\\main\\resources", "");
						  tempTargetPath= "IFSXface\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("core\\IFSCore\\src\\main\\java", "IFSCore\\target\\classes");
					      sourcePath=new File(source+ "core\\" +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("core\\IFS\\"))
					  {						 
						  String f1 = filePath.replace("core\\IFS\\src\\main\\java", "");
						  f1 = f1.replace("core\\IFS\\src\\main\\resources", "");
						  tempTargetPath= "IFS\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("core\\IFS\\src\\main\\java", "IFS\\target\\classes");
					      sourcePath=new File(source+ "core\\" +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("core\\IFSEntity-Base\\"))
					  {						 
						  String f1 = filePath.replace("core\\IFSEntity-Base\\src\\main\\java", "");
						  f1 = f1.replace("core\\IFSEntity-Base\\src\\main\\resources", "");
						  tempTargetPath= "IFSEntity-Base\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);
					      
					      tempSourcePath=filePath.replace("core\\IFSEntity-Base\\src\\main\\java", "IFSEntity-Base\\target\\classes");
					      sourcePath=new File(source+ "core\\" +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("Front\\IFSFront\\"))
					  {						 
						  String f1 = filePath.replace("Front\\IFSFront\\src\\main\\java", "");
						  f1 = f1.replace("Front\\IFSFront\\src\\main\\resources", "");
						  tempTargetPath= "IFSFront\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("Front\\IFSFront\\src\\main\\java", "Front\\IFSFront\\target\\classes");
					      sourcePath=new File(source+ "" +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("disclosure\\IFSInfoDisclosure\\"))
					  {						 
						  String f1 = filePath.replace("disclosure\\IFSInfoDisclosure\\src\\main\\java", "");
						  f1 = f1.replace("disclosure\\IFSInfoDisclosure\\src\\main\\resources", "");
						  tempTargetPath= "IFSInfoDisclosure\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("disclosure\\IFSInfoDisclosure\\src\\main\\java", "disclosure\\IFSInfoDisclosure\\target\\classes");
					      sourcePath=new File(source+ "" +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
						  
					  }else if( filePath.startsWith("payment\\PaymentServer\\"))
					  {						 
						  String f1 = filePath.replace("payment\\PaymentServer\\src\\main\\java", "");
						  f1 = f1.replace("payment\\PaymentServer\\src\\main\\resources", "");
						  tempTargetPath= "PaymentServer\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("payment\\PaymentServer\\src\\main\\java", "PaymentServer\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if( filePath.startsWith("crm\\Crm\\"))
					  {						 
						  String f1 = filePath.replace("crm\\Crm\\src\\main\\java", "");
						  f1 = f1.replace("crm\\Crm\\src\\main\\resources", "");
						  tempTargetPath= "Crm\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("crm\\Crm\\src\\main\\java", "Crm\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if( filePath.startsWith("crm\\CrmServer\\"))
					  {						 
						  String f1 = filePath.replace("crm\\CrmServer\\src\\main\\java", "");
						  f1 = f1.replace("crm\\CrmServer\\src\\main\\resources", "");
						  tempTargetPath= "CrmServer\\WEB-INF\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("crm\\CrmServer\\src\\main\\java", "CrmServer\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if( filePath.startsWith("H5\\IFSH5api\\")){						 
						  String f1 = filePath.replace("H5\\IFSH5api\\src\\main\\java", "");
						  f1 = f1.replace("H5\\IFSH5api\\src\\main\\resources", "");
						  tempTargetPath= "H5\\IFSH5api\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("H5\\IFSH5api\\src\\main\\java", "IFSH5api\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if( filePath.startsWith("Payment\\Payment\\")){						 
						  String f1 = filePath.replace("Payment\\Payment\\src\\main\\java", "");
						  f1 = f1.replace("Payment\\Payment\\src\\main\\resources", "");
						  tempTargetPath= "Payment\\Payment\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("Payment\\Payment\\src\\main\\java", "Payment\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else if( filePath.startsWith("Tranplatform\\Tranplatform\\")){						 
						  String f1 = filePath.replace("Tranplatform\\Tranplatform\\src\\main\\java", "");
						  f1 = f1.replace("Tranplatform\\Tranplatform\\src\\main\\resources", "");
						  tempTargetPath= "Tranplatform\\Tranplatform\\classes"+f1;						   
					      targetPath=new File(target+tempTargetPath);

					      tempSourcePath=filePath.replace("Tranplatform\\Tranplatform\\src\\main\\java", "Tranplatform\\Tranplatform\\target\\classes");
					      sourcePath=new File(source +tempSourcePath);
					      copy.copyFolder(sourcePath,targetPath); 
					  }else{
						  throw new IFSException(new Exception(),"999999","路径错误\n:" + filePath);
					  }
					  								
				  }
				  
				}
				else
                { 				
				  sourcePath=new File(source+filePath);
				  targetPath=new File(target+filePath);
				  copy.copyFolder(sourcePath,targetPath);
				  if(copyCodeFlag == false){
					  successCopyCode ++;
				  }
				}
			}
			br.close();
			reader.close();
		} catch (Exception e) {
			
			System.out.println("sourcePath:"+source+filePath);
			System.out.println("targetPath:"+target+filePath);
			e.printStackTrace();
		}
		if(notExitFileList.size() != 0){
			System.out.println("以下文件不存在: \n");
			for (String path : notExitFileList) {
				System.out.println(path);
			}
		}
	}
	
	/**
	 * 复制代码
	 * @param src
	 * @param dest
	 * @throws IOException
	 */
	private void copyFolder(File src, File dest) throws IOException {
		File destDir =dest.getParentFile();
		    
			if (!destDir.exists()) {				
				 destDir.mkdirs();
			}
			
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dest);

			byte[] buffer = new byte[1024];

			int length;
			
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		
	}

}
